// ----------------------------------
// RSDK Project: Sonic 1 Forever X Doki Doki Literature Club
// Script Description: Opening Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 1 : TYPE_SEGA
public alias 256 : GROUP_PLAYERS
public alias 65536 : HITBOX_AUTO
public alias arrayPos6 : currentPlayer
public alias arrayPos7 : playerCount

private alias object.value13 : reset.done

//Character Object
private alias object.value0 : object.timer
private alias object.value1 : object.xMonika
private alias object.value2 : object.yMonika
private alias object.value3 : object.sMonika
private alias object.value4 : object.xSayori
private alias object.value5 : object.ySayori
private alias object.value6 : object.sSayori
private alias object.value7 : object.xYuri
private alias object.value8 : object.yYuri
private alias object.value9 : object.sYuri
private alias object.value10 : object.xNat
private alias object.value11 : object.yNat
private alias object.value12 : object.sNat


public alias saveRAM[257] : saved.spindash
public alias saveRAM[258] : saved.speedCap
public alias saveRAM[259] : saved.airSpeedCap
public alias saveRAM[260] : saved.spikeBehavior
public alias saveRAM[261] : saved.shieldType
public alias saveRAM[262] : saved.superStates
public alias saveRAM[263] : saved.Jumplock
public alias saveRAM[264] : saved.Supermusic
public alias saveRAM[265] : saved.Dropdash
public alias saveRAM[266] : saved.Instashield
public alias saveRAM[267] : saved.Peelout
public alias saveRAM[268] : saved.FlightCancel
public alias saveRAM[269] : saved.SonicMaxDash
public alias saveRAM[270] : saved.Speen
public alias saveRAM[271] : saved.Supercancel
public alias saveRAM[272] : saved.Drowning
public alias saveRAM[273] : saved.Timer
public alias saveRAM[274] : saved.region
public alias saveRAM[275] : saved.UFO
public alias saveRAM[276] : saved.rotate
public alias saveRAM[277] : saved.invincibilityType
public alias saveRAM[280] : saved.gogoplus
public alias saveRAM[281] : saved.lives
public alias saveRAM[282] : saved.bubble
public alias saveRAM[283] : saved.specialSpin
public alias saveRAM[284] : saved.timeLimit
public alias saveRAM[285] : saved.GHZwater
public alias saveRAM[286] : saved.language
public alias saveRAM[287] : saved.elemental

public alias saveRAM[290] : saved.randomMonitor

public alias saveRAM[294] : saved.graphics
public alias saveRAM[295] : saved.mobile
public alias saveRAM[296] : saved.transmobile

public alias saveRAM[297] : saved.TailsMaxDash
public alias saveRAM[298] : saved.KnuxMaxDash
public alias saveRAM[299] : saved.AmyMaxDash
public alias saveRAM[300] : saved.SuperMaxDash

public alias saveRAM[301] : saved.AmyMoveset

public alias saveRAM[312] : saved.dokiTracks
public alias saveRAM[313] : saved.dokiMenuMusic
public alias saveRAM[314] : saved.SayoriMaxDash
public alias saveRAM[315] : saved.NatsukiMaxDash
public alias saveRAM[316] : saved.YuriMaxDash
public alias saveRAM[317] : saved.MonikaMaxDash

public alias saveRAM[302] : saved.lampStyle
public alias saveRAM[303] : saved.bulbStyle
public alias saveRAM[304] : saved.bulbYellow
public alias saveRAM[305] : saved.sonic2entrance
public alias saveRAM[306] : saved.beeToggle
public alias saveRAM[307] : FirstBootWarning

public alias saveRAM[408] : emeraldGauntletUnlock
public alias saveRAM[410] : bossRushUnlock

private table MenuControl_RandomCharacters
	6 // Space
	0 // @
	4 // . and !
	1 // tiny randos
end table

private table MenuControl_TinyLetterWidth
	4, 3, 4, 4, 3, 3, 4, 4, 3, 3, 3, 3, 5, 4, 4, 3, 5, 3, 3, 3, 4, 5, 5, 3, 3, 3 // upper
	4, 3, 3, 3, 3, 2, 3, 3, 1, 3, 3, 1, 5, 3, 3, 3, 4, 2, 3, 3, 3, 3, 5, 3, 3, 3 // lower
	3, 3, 3, 3, 3, 3, 3, 3, 3, 3 // numbers
end table

// Function declarations
reserve function Save_Transfer

function Save_Transfer
	if saveRAM[4] == 0
	
	else
		saveRAM[1000] = saveRAM[0]
		saveRAM[1001] = saveRAM[1]
		saveRAM[1002] = saveRAM[2]
		saveRAM[1003] = saveRAM[3]
		saveRAM[1004] = saveRAM[4]
		saveRAM[1005] = saveRAM[5]
		saveRAM[1006] = saveRAM[6]
		saveRAM[1007] = saveRAM[7]
	end if
	
	
	if saveRAM[12] == 0
	
	else	
		saveRAM[1008] = saveRAM[8]
		saveRAM[1009] = saveRAM[9]
		saveRAM[1010] = saveRAM[10]
		saveRAM[1011] = saveRAM[11]
		saveRAM[1012] = saveRAM[12]
		saveRAM[1013] = saveRAM[13]
		saveRAM[1014] = saveRAM[14]
		saveRAM[1015] = saveRAM[15]
	end if
	
	
	if saveRAM[20] == 0
	
	else	
		saveRAM[1016] = saveRAM[16]
		saveRAM[1017] = saveRAM[17]
		saveRAM[1018] = saveRAM[18]
		saveRAM[1019] = saveRAM[19]
		saveRAM[1020] = saveRAM[20]
		saveRAM[1021] = saveRAM[21]
		saveRAM[1022] = saveRAM[22]
		saveRAM[1023] = saveRAM[23]
	end if
	
	
	if saveRAM[28] == 0
	
	else	
		saveRAM[1024] = saveRAM[24]
		saveRAM[1025] = saveRAM[25]
		saveRAM[1026] = saveRAM[26]
		saveRAM[1027] = saveRAM[27]
		saveRAM[1028] = saveRAM[28]
		saveRAM[1029] = saveRAM[29]
		saveRAM[1030] = saveRAM[30]
		saveRAM[1031] = saveRAM[31]
	end if
	
	WriteSaveRAM()
	PlaySfx(SfxName[Lamp Post], 0)
end function


event ObjectMain
	if reset.done == 0
		stage.playerListPos = 0
		credits.screen = 0
		stage.debugMode = 0
		options.stageSelectFlag = 0
		options.gameMode = 0
		options.attractMode = 0
		player.lives = 3
		player.continues = 0
		player.score = 0
		player.scoreBonus = 0xC350
		stage.player2Enabled = 0
		lampPostID = 0
		specialStage.listPos = 0
		specialStage.emeralds = 0
		specialStage.nextZone = 0
		
		//if screen.xsize != 320
		//	CallNativeFunction2(SetScreenWidth, 426, 0)
		//end if
		
		WriteSaveRAM()
		reset.done = 1
	end if
	
	if inputPress.up == 1
		object.value16++
		if object.value16 == 5
			CallFunction(Save_Transfer)
		end if
	end if

	if MenuIntroSkip == 1
		PlayMusic(0)
		ResetObjectEntity(object[+2].entityPos, TypeName[Menu Control], 0, 0, 0)				
		ResetObjectEntity(object.entityPos, TypeName[Sidebar], 0, 0, 0)
		object.ixpos = 0
		object[+2].value0 = 256
		object.value2 = 20
		object.state = 3
		object.drawOrder = 5
		SetClassicFade(0, 0, 0, 256)
		arrayPos0 = object[+1].entityPos
		ResetObjectEntity(arrayPos0, TypeName[Characters], 0, 0, 0)
		object[arrayPos0].drawOrder = object.drawOrder
		object[arrayPos0].drawOrder--		
	else		
		switch object.state
		case 0			
			object.value0 = 320
			SetClassicFade(color7, color7, color7, object.value0)
			object.state++
			break
		case 1
			if object.value0 > 0
				object.value0 -= 8
			else
				object.value0 = 0
				object.state++
			end if
			SetClassicFade(color7, color7, color7, object.value0)
			break
		case 2
			object.value0++
			if object.value0 >= 140
				object.value0 = 0
				object.state = 3
			end if
			if object.propertyValue == 0
				if object.value0 == 2
					PlayMusic(0)	
				end if
			end if	
			break
		case 3
			if object.value0 < 256
				object.value0 += 8
				SetClassicFade(color7, color7, color7, object.value0)
			else
				if object.propertyValue == 0
					object.state = 0
					object.propertyValue = 1
				else
					ResetObjectEntity(object[+2].entityPos, TypeName[Menu Control], 0, 0, 0)
					arrayPos0 = object[+1].entityPos
					ResetObjectEntity(arrayPos0, TypeName[Characters], 0, 0, 0)
					object[arrayPos0].drawOrder = object.drawOrder
					object[arrayPos0].drawOrder--						
					object.state++
				end if
				SetClassicFade(color7, color7, color7, 255)
			end if
			break
		case 4
			if object.value0 > 0
				object.value0 -= 8
			else
				object.value0 = 0
				object.state++
			end if
			SetClassicFade(color7, color7, color7, object.value0)
			break
		case 5
			object.value5++
			if object.value5 == 40
				ResetObjectEntity(object.entityPos, TypeName[Sidebar], 0, 0, 0)
				object.ixpos = 120
				object.value2 = -100
				object.drawOrder = 5
			end if
			break
		end switch
	end if	
end event


event ObjectDraw
	if object.state < 4 
		DrawRect(0, 0, screen.xsize, screen.ysize, color7, color7, color7, 255)
		if object.propertyValue == 0
			if object.state > 0
				DrawSpriteScreenXY(69, screen.xcenter, screen.ycenter)
				DrawSpriteScreenXY(70, screen.xcenter, screen.ycenter)
			end if	
		else
			temp0 = screen.xcenter
			temp7 = 0
			object.value1 = 0
			arrayPos0 = 0
			GetTextInfo(temp1, 2, TEXTINFO_TEXTSIZE, object.value1, 0)

			// Now cycle through the letters in the string
			while temp1 > 0
				GetTextInfo(object.value3, 2, TEXTINFO_TEXTDATA, object.value1, arrayPos0)
					if object.value3 >= 65
						if object.value3 <= 90
							object.frame += 41
							temp3 = MenuControl_TinyLetterWidth
							temp5 = object.value3
							temp5 -= 65
						end if
					end if

					if object.value3 >= 97
						if object.value3 <= 122
							object.frame += 35
							temp3 = MenuControl_TinyLetterWidth
							temp5 = object.value3
							temp5 -= 97
							temp5 += 26
						end if
					end if

					if object.value3 == 32
						object.frame = 0
						temp3 = MenuControl_TinyLetterWidth
						temp5 = 1
					end if

					if object.value3 == 64
						object.frame = 0
						temp0 = screen.xcenter
						temp3 = MenuControl_RandomCharacters
						temp5 = 1				
					end if

					if object.value3 == 46
						object.frame = 205
						temp3 = MenuControl_RandomCharacters
						temp5 = 3				
					end if

				if arrayPos0 == 0
					GetTableValue(temp4, temp5, temp3)
					temp7 += temp4
					temp7++
				end if

				// Move on to the next letter that we've stored
				arrayPos0++

				// Then decrease the amount of letters we have left
				temp1--

				// Finally, space out each character
				if temp1 > 0
					GetTableValue(temp4, temp5, temp3)
					temp7 += temp4
					temp7++
				end if
			loop
			temp7 /= 2
			temp0 -= temp7

			arrayPos0 = 0

			GetTextInfo(temp1, 2, TEXTINFO_TEXTSIZE, object.value1, 0)

			// Now cycle through the letters in the string
			while temp1 > 0
				GetTextInfo(object.value3, 2, TEXTINFO_TEXTDATA, object.value1, arrayPos0)
				object.frame = object.value3

				if object.value3 >= 65
					if object.value3 <= 90
						object.frame -= 65
						temp3 = MenuControl_TinyLetterWidth
						temp5 = object.value3
						temp5 -= 65
					end if
				end if

				if object.value3 >= 97
					if object.value3 <= 122
						object.frame -= 71
						temp3 = MenuControl_TinyLetterWidth
						temp5 = object.value3
						temp5 -= 97
						temp5 += 26
					end if
				end if

				if object.value3 >= 48
					if object.value3 <= 57
						object.frame += 11
						temp3 = MenuControl_TinyLetterWidth
						temp5 = object.value3
						temp5 -= 48
						temp5 += 26
						temp5 += 26
					end if
				end if

				if object.value3 == 32
					object.frame = 58
					temp3 = MenuControl_TinyLetterWidth
					temp5 = 1
				end if

				if object.value3 == 64
					object.frame = 58
					temp0 = screen.xcenter
					temp0--
					temp3 = MenuControl_RandomCharacters
					temp5 = 1				
				end if
				if object.value3 == 46
					object.frame = 54
					temp3 = MenuControl_RandomCharacters
					temp5 = 3				
				end if			
				if object.value3 == 44
					object.frame = 55
					temp3 = MenuControl_RandomCharacters
					temp5 = 3				
				end if			
				if object.value3 == 33
					object.frame = 57
					temp3 = MenuControl_RandomCharacters
					temp5 = 3				
				end if			
				if object.value3 == 39
					object.frame = 56
					temp3 = MenuControl_RandomCharacters
					temp5 = 3				
				end if			

				// Now we'll start drawing
				DrawSpriteScreenXY(object.frame, temp0, screen.ycenter)

				// Move on to the next letter that we've stored
				arrayPos0++

				// Then decrease the amount of letters we have left
				temp1--

				// Finally, space out each character
				GetTableValue(temp4, temp5, temp3)
				temp0 += temp4
				temp0++
			loop
		end if	
	end if	
end event


event ObjectStartup
	ReadSaveRAM()

	
	switch saved.dokiMenuMusic
	case 1
		SetMusicTrack("DokiDokiGenesis.ogg", 0, 770570)		
		break
	case 0
		SetMusicTrack("DokiDokiGreenHill.ogg", 0, 1539232)		
		break
	case 2
		SetMusicTrack("DokiDoki.ogg", 0, 962839)		
		break
	case 3
		SetMusicTrack("DreamsIntoReality.ogg", 0, 17661)	
		break
	end switch

	LoadSpriteSheet("DDLCMenu/Menu.gif")

	//Uppercase Tiny Font | 0-25
	temp0 = 0
	temp2 = 1
	temp3 = 242
	while temp0 < 2
		temp1 = 0
		while temp1 < 13
			SpriteFrame(0, 0, 5, 5, temp2, temp3) 
			temp1++
			temp2 += 5
		loop
		temp2 = 1
		temp0++
		temp3 += 6
	loop

	//Lowercase Tiny Font | 26-51
	temp0 = 0
	temp2 = 1
	temp3 = 254
	while temp0 < 2
		temp1 = 0
		while temp1 < 13
			SpriteFrame(0, 0, 5, 7, temp2, temp3) 
			temp1++
			temp2 += 5
		loop
		temp2 = 1
		temp0++
		temp3 += 7
	loop

	SpriteFrame(0, 0, 15, 12, 286, 255)  // 52 . small font
	SpriteFrame(0, 0, 15, 12, 302, 255)  // 53 ! small font
	SpriteFrame(0, 0, 1, 6, 52, 269) // 54 . 
	SpriteFrame(0, 0, 1, 6, 54, 269) // 55 , 
	SpriteFrame(0, 0, 1, 6, 56, 269) // 56 ' 
	SpriteFrame(0, 0, 1, 6, 58, 269) // 57 ! 	
	SpriteFrame(0, 0, 0, 0, 0, 0) // 58

	// tiny numbers 59-68
	temp1 = 0
	temp2 = 1
	while temp1 < 10
		SpriteFrame(0, 0, 5, 5, temp2, 269) 
		temp1++
		temp2 += 5
	loop
	SpriteFrame(-94, -22, 188, 58, 133, 140) // 69	
	SpriteFrame(-57, -40, 114, 14, 152, 121) // 70
	options.touchControls = 0
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
